<?php

/**
 * This is the model class for table "web_coupon".
 *
 * The followings are the available columns in table 'web_coupon':
 * @property string $id
 * @property string $site_key
 * @property string $product_type
 * @property string $min_money
 * @property string $coupon_type
 * @property string $coupon_value
 * @property string $start_time
 * @property string $over_time
 * @property integer $use_num
 * @property string $uid
 * @property integer $status
 */
class WebCoupon extends CActiveRecord {

    public $couponTypes = array(
        'percent' => '打折',
        'offset' => '抵现',
    );
    
    public $status_array = array(
        1=>'有效',
        2=>'无效'
    );

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return WebCoupon the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'web_coupon';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('coupon, coupon_value,coupon_type', 'required'),
            array('coupon', 'unique'),
            array('use_num, status, game_id', 'numerical', 'integerOnly' => true),
            array('start_time, over_time, min_money', 'safe'),
            array('id, product_type, min_money, coupon_type, coupon_value, start_time, over_time, use_num, uid, status, coupon, game_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'coupon' => '打折码',
            'product_type' => '产品类型',
            'min_money' => '最小使用金额',
            'coupon_type' => '优惠方式',
            'coupon_value' => '优惠额度',
            'start_time' => '开始时间',
            'over_time' => '结束时间',
            'use_num' => '可用次数',
            'uid' => '推广人员编号',
            'status' => '状态',
            'game_id' => '游戏'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('game_id', $this->game_id);
        $criteria->compare('product_type', $this->product_type);
        $criteria->compare('coupon_type', $this->coupon_type);
        $criteria->compare('coupon_value', $this->coupon_value);
        $criteria->compare('start_time', $this->start_time);
        $criteria->compare('over_time', $this->over_time);
        $criteria->compare('uid', $this->uid);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    
    /**
     * 查询打折码是否存在
     * @param string $coupon
     * @return boolean
     */
    public function checkCoupon($coupon){
        $cr = new CDbCriteria;
        $cr->compare('coupon', $coupon);
        $cr->addCondition('use_num>0 OR use_num=-1');
        $data = WebCoupon::model()->find($cr);
        $now = time();
        
        if($data){
            $data->start_time = strtotime($data->start_time);
            $data->over_time = strtotime($data->over_time);

            if($data->start_time == 0 || $data->start_time <= $now){
                if($data->over_time == 0 || $data->over_time >= $now){
                    return TRUE;//Coupon 存在并有效
                }  else {
                    return FALSE;
                }
            }else{
                return FALSE;
            }
        }else{
            return FALSE;
        }
    }

}